package com.superzlc.vjoy.socket;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.LinkedList;

import com.superzlc.vjoy.VJoyServer;
import com.superzlc.vjoy.socket.MessageOp.DataPackage;
import com.superzlc.vjoy.socket.MessageOp.HeartbeatPackage;
import com.superzlc.vjoy.socket.MessageOp.NormalAckpackage;
import com.superzlc.vjoy.socket.MessageOp.SimplePackage;
import com.superzlc.vjoy.socket.MessageOp.VJoyPackage;

public class MessageServer {
	ServerSocket server;
	LinkedList<Socket> list = new LinkedList<>();
	Thread tserver;
	boolean isStarted = false;

	public void startup() {
		try {
			server = new ServerSocket(Constants.PORT_FOR_MESSAGE);
		} catch (IOException e1) {
			System.out.println("启动消息服务端出错，" + e1.getMessage());
			e1.printStackTrace();
			return;
		}
		tserver = new Thread() {
			public void run() {
				while (isStarted && !server.isClosed()) {
					Socket s;
					try {
						s = server.accept();
					} catch (IOException e) {
						System.out.println("消息服务端接受连接出错，" + e.getMessage());
						e.printStackTrace();
						continue;
					}
					list.add(s);
					new Thread(new SocketThread(s)).start();
				}
			}
		};
		tserver.start();
		isStarted = true;
	}

	public void shutdown() {
		isStarted = false;
		try {
			server.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		tserver.interrupt();
	}

	class SocketThread implements Runnable {
		Socket socket;
		OutputStream os;
		InputStream is;
//		DataOutputStream dos;
//		DataInputStream dis;

		public SocketThread(Socket socket) {
			this.socket = socket;
		}

		NormalAckpackage normalAckpackage = new NormalAckpackage();

		@Override
		public void run() {
			try {
				System.out.println("开始获取sock数据流...");
				os = socket.getOutputStream();
				is = socket.getInputStream();
//				dos = new DataOutputStream(os);
//				dis = new DataInputStream (is);
			} catch (IOException e) {
				System.out.println("消息服务端获取输入输出流出错，" + e.getMessage());
				e.printStackTrace();
				return;
			}
			try {
				System.out.println("开始接收和处理客户端发来的数据包...");
				byte[] buffer = new byte[1024 * 10];
				while(isStarted && !socket.isClosed()) {
					DataPackage p = MessageOp.read(is, buffer);
					if (p == null) {
						System.out.println(System.currentTimeMillis() + " 接收到的数据包为空");
						continue;
					}
					System.out.println(System.currentTimeMillis() + " 接收到 " + p.toString());
					switch(p.getType()) {
					case SimplePackage.type:
						MessageOp.sendData(os, normalAckpackage);
						break;
					case NormalAckpackage.type:
						// MessageOp.sendData(dos, normalAckpackage);
						break;
					case HeartbeatPackage.type:
						MessageOp.sendData(os, normalAckpackage);
						break;
					case VJoyPackage.type:
						if (p != null)
							setVJoy((VJoyPackage)p);
						MessageOp.sendData(os, normalAckpackage);
						break;
					}
					Thread.sleep(5);
				}
			} catch (IOException e) {
				System.out.println("消息服务端处理流数据出错，" + e.getMessage());
				e.printStackTrace();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}

	}

	VJoyServer vjoy;

	public void setVjoyServer(VJoyServer vjoy) {
		this.vjoy = vjoy;
	}
	long timeStart = System.currentTimeMillis();
	public void setVJoy(VJoyPackage pkg) throws IOException {
		vjoy.ctrl(pkg);
	}
}
